package bomberman.model;

import java.awt.Graphics;
import java.awt.Rectangle;

import bomberman.MainGame;
import bomberman.runable.MyRunableBom;
import bomberman.runable.MyRunableBomChange;
import bomberman.swing.BomJPanel;
import bomberman.util.CommonTouch;
import bomberman.util.Constants;
import bomberman.util.GameMap;

/**
 * 炸弹
 * @author EX-FUHUICHAO001 
 * 2016-9-22 13:39:25
 */
public class Bom {
	private int x;
	private int y;
	
	// 定义是否已爆炸
	private boolean isBoom = false;
	
	// 定义炸弹大小
	private int size = Constants.BOM_SIZE_BIG;
	
	/**
	 * 根据坐标判断炸弹安装位置，若为边界，则靠右
	 */
	public Bom(int x, int y) {
		this.x = (x + 30 / 2) / 30 * 30;
		this.y = (y + 30 / 2) / 30 * 30;
	}

	public Rectangle getRect(){
		return new Rectangle(x, y, 30, 30);
	}
	
	public void draw(Graphics g) {
		g.drawImage(GameMap.img_bom_array[size], x, y, MainGame.jPanelIndex);
	}
	
	/**
	 * 爆炸
	 * 移出bom_bounds_sets，后续不做该炸弹的碰撞检测
	 * 移出boms，后续不现在在地图
	 */
	public void boom(){
		if(!isBoom){// 避免重复调用爆炸方法
			String bound = x + "," + y;
			if(BomJPanel.bom_bounds_sets.contains(bound)){
				BomJPanel.bom_bounds_sets.remove(bound);
			}
			if(BomJPanel.boms.contains(this)){
				BomJPanel.boms.remove(this);
			}
			isBoom = true;
			
			// 添加火焰
			new Flame(x, y, Constants.FLAME_STYLE_MIX).addFlame();
		}
	}
	
	
	/**
	 * 添加炸弹，并开启线程
	 */
	public void addBom(){
		String set = x + "," + y;
		// 位置有炸弹则不重复放置
		// 引申逻辑：若人在原地未动，待炸弹爆炸后可以在原地继续放炸弹（原地的炸弹一定是最后一个爆炸的，即BomJPanel.boms.size() == 0）
		if (BomJPanel.boms.size() < Constants.BOM_NUMBER 
				&& (BomJPanel.boms.size() == 0 || "".equals(BomJPanel.temp_bounds)) 
				&& !CommonTouch.checkIfBomTouchEnemy(this)) {
			BomJPanel.temp_bounds = set;
			BomJPanel.boms.add(this);
			
			// 开启线程控制大小变化和爆炸
//			MyRunableThreadPool.getExecutorService().execute(new MyRunableBomChange(this));
//			MyRunableThreadPool.getExecutorService().execute(new MyRunableBom(this));
			new Thread(new MyRunableBomChange(this)).start();
			new Thread(new MyRunableBom(this)).start();
		}
	}
	
	

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public boolean isBoom() {
		return isBoom;
	}

	public void setBoom(boolean isBoom) {
		this.isBoom = isBoom;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

}
